<?php

namespace app\api\server\user;

use app\api\model\user\UserMusicApplyApiModel;
use app\api\ServerBase;
use app\api\validate\user\UserMusicApplyVal;
use think\db\exception\DbException;
use think\facade\Db;

class UserMusicApplyServer extends ServerBase
{

    function __construct($request)
    {
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new UserMusicApplyApiModel();
        $this->validate = new UserMusicApplyVal();
        if($this->request->user['is_writer'] != 1){
            $this->ERROR='请先成为创作者';
            return false;
        }
    }
    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $field= 'id,type,title,bpm,key,img,url,style,pan_url,pan_pwd,audit,audit_remark';
        $this->param['user_id'] = $this->request->user_id;
        [$list,$count] = $this->model->getApiList($this->param,$field,$page,$limit);
        $result['data']=$list;
        $result['count']=$count;
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function add(){
        $post = $this->request->post();
        $post['user_id'] = $this->request->user_id;
        if(!$this->validate->scene('create')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        Db::startTrans();
        try {
            //验证商品是否存在和type是否合法
            $this->model->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        $info  = $this->model->getFindById($id);
        if($info['user_id'] != $this->request->user_id){
            $this->getMessageCode('code_common.illegal',1);
            return false;
        }
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        if($info['audit'] == 20){
            $this->ERROR='已通过，不能修改';
            return false;
        }
        $post['user_id'] = $this->request->user_id;
        if(!$this->validate->scene('update')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        Db::startTrans();
        try {
            $post['audit'] = 10;
            $info->save($post);
            Db::commit();
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }catch (\Error $error){
            Db::rollback();
            $this->ERROR = $error->getMessage();
            return false;
        }catch (DbException $dbException){
            Db::rollback();
            $this->ERROR = $dbException->getMessage();
            return false;
        }
    }
}